package com.zs.framework.mail.utils;

import com.zs.framework.mail.exception.MailClientException;
import lombok.extern.slf4j.Slf4j;

import java.util.Properties;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author zhougf
 * @version 1.0
 * @date 2020/07/27
 */
@Slf4j
public class SendEmailProducer {

    private static SendEmailProducer sendEmailProducer = null;

    private SendEmailProducer() {
    }

    public static SendEmailProducer getInstance() {
        if(sendEmailProducer != null) {
            return sendEmailProducer;
        }
        sendEmailProducer = new SendEmailProducer();
        return sendEmailProducer;
    }

    private Properties properties;

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public boolean sendMail(String theme, String content) {
        if (null == this.properties) {
            throw new MailClientException("Create a general SendEmailProducer failure properties is null");
        }
        String receiveAddress = this.properties.getProperty("receiveAddress");
        String sendAddress = this.properties.getProperty("sendAddress");
        String userName = this.properties.getProperty("userName");
        String userPassword = this.properties.getProperty("userPassword");
        String host = this.properties.getProperty("host");
        Integer port = Integer.valueOf(this.properties.getProperty("port"));
        String protocol = this.properties.getProperty("protocol");

        // 定义properties对象，设置环境信息
        Properties properties = new Properties();
        /*
         * mail.smtp.host ：指定连接的邮件服务器的主机名。如：163邮箱就填写smtp.163.com
         * 若在本地测试的话，需要在本地安装smtp服务器
         */
        properties.setProperty("mail.smtp.host", host);
        // mail.smtp.auth：指定客户端是否要向邮件服务器提交验证
        properties.setProperty("mail.smtp.auth", "true");
        /*
         * mail.transport.protocol：指定邮件发送协议：smtp。smtp：发邮件；pop3：收邮件
         * mail.store.protocol:指定邮件接收协议
         */
        properties.setProperty("mail.transport.protocol", protocol);
        // 获取session对象
        Session session = Session.getInstance(properties);
        // 当设置为true，JavaMail AP就会将其运行过程和邮件服务器的交互命令信息输出到console中，用于JavaMail的调试
        session.setDebug(true);
        try {
            Transport transport = session.getTransport();

            // 创建邮件对象
            MimeMessage message = new MimeMessage(session);

            // 设置邮件发送方
            message.setFrom(new InternetAddress(sendAddress));

            // 设置邮件发送的主题<邮件标题>
            message.setSubject(theme);

            // 设置邮件发送的内容
            message.setContent(content, "text/html;charset=utf-8");
            // 连接邮件服务器，“”中填写邮件服务器主机名
            transport.connect(host, port, userName, userPassword);
            transport.sendMessage(message, new Address[]{new InternetAddress(receiveAddress)});
            transport.close();
            return true;
        } catch (MessagingException e) {
            log.info("send mail exception", e);
            return false;
        }
    }
}
